package Servlet.admin;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.AdminService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet("/admin/addNews")
public class AddNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../admin/login.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Long newsTypeId = 0L;
        String newsTitle = null;
        String newsContent = null;
        String newsDateTime = null;
        String fileName = null;

        DiskFileItemFactory factory = new DiskFileItemFactory();

//        factory.setCharacterEncoding("UTF-8");
        ServletContext context = request.getServletContext();
        File f = new File(context.getInitParameter("tmp"));


        if (!f.exists()){
            f.mkdirs();
        }
        factory.setRepository(f);

        ServletFileUpload fileUpload = new ServletFileUpload(factory);

        try {
            List<FileItem> fileItems = fileUpload.parseRequest(request);
            for (FileItem fileItem : fileItems) {
                fileName = fileItem.getFieldName();
                if (fileItem.isFormField()) {
                    System.out.println("普通文件");
                    if ("newsTypeId".equals(fileName)){
                        newsTypeId = Long.parseLong(fileItem.getString());
                    }
                    if ("title".equals(fileName)){
                        newsTitle = fileItem.getString("UTF-8");
                    }
                    if ("content".equals(fileName)){
                        newsContent = fileItem.getString("UTF-8");
                    }
                    if ("datetime".equals(fileName)){
                        newsDateTime = fileItem.getString();
                    }
                }else {
                    fileName = fileItem.getName();
                    String s = UUID.randomUUID().toString();
                    fileName = s+fileName.substring(fileName.lastIndexOf("."));
                    FileOutputStream out = new FileOutputStream("D:\\app_bc\\Run\\JavaWeb\\javaweb\\web\\images\\content-images\\" + fileName);
                    InputStream inputStream = fileItem.getInputStream();
                    int len = 0;
                    byte[] buffer = new byte[1024];
                    while((len=inputStream.read(buffer))>0){
                        out.write(buffer, 0, len);
                    }
                    inputStream.close();
                    out.close();
                }
            }
        } catch (FileUploadException e) {
            throw new RuntimeException(e);
        }

//        AdminService.insertData(newsTypeId,newsTitle,newsContent,newsDateTime,fileName);

        System.out.println(newsTypeId);
        System.out.println(newsTitle);
        System.out.println(newsContent);
        System.out.println(newsDateTime);
        System.out.println("fileName = " + fileName);
        response.sendRedirect(request.getContextPath()+"/admin/newsManagement");
    }
}
